<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta content="phalconplus API doc" name="description">
    <title>
                    phalconplus API Documentation
            </title>
    <link rel="stylesheet" type="text/css" href="../../../asset/static/prettify.css" />

<script src="../../../asset/static/prettify.js"></script>
<script src="../../../asset/api_definition.js"></script>


                        <link rel="stylesheet" type="text/css" href="../../../asset/static/prettify.css" />
                    <link rel="stylesheet" type="text/css" href="../../../asset/static/zephir-theme.css" />
            
    

                        <script src="../../../asset/static/prettify.js"></script>
                                    <script src="../../../asset/api_definition.js"></script>
                    <script src="../../../asset/static/jquery.min.js"></script>
                    <script src="../../../asset/static/zephir-doc.js"></script>
                <script type="application/javascript">let ZepCurrentPath = '../../../'</script>
    </head>
 <body onload="prettyPrint()">
        <div id="top-bar">
            <div id="top-left">
                                    phalconplus Documentation <span class="version">v1.2.0</span>
                            </div>
            <div id="top-right">
            </div>
        </div>
        <div id="body">
            <div id="body-left">
                <div class="header-search">
                    <input class="search-box" placeholder="Search for a class or namespace " type="text" />
                    <div id="menu-wrapper">
                    </div>
                </div>
            </div>
            <div id="body-right">
                <div class="content">
                    
<div class="breadcrumb">
    
    <ul>
        
        <li><a href="../../../index.html">Home</a></li>
        
                <li><a href="../../../classes.html">Classes</a></li>
                <li><a href="../../../class/PhalconPlus/Http/PsrResponseFactory.html">PhalconPlus\Http\PsrResponseFactory</a></li>
                <li><a >Source</a></li>
                
    </ul>
    
</div>
<div class="class-header source clearfix">
    
    <div class="access-buttons">
        <a class="zep-button" href="../../../class/PhalconPlus/Http/PsrResponseFactory.html">Class</a>

            </div>
    
    <div class="class-full-name">
        <span class="class-type">Class</span>
        
                <span class="namespace-piece">
            <a href="../../../namespace/PhalconPlus.html">PhalconPlus</a>
        </span>
                <span class="namespace-piece">
            <a href="../../../namespace/PhalconPlus/Http.html">Http</a>
        </span>
        
        <h1 class="class-name">PsrResponseFactory</h1>
    </div>
    
</div>

<pre class="zephir-source-file prettyprint linenums">
    namespace PhalconPlus\Http;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;
use GuzzleHttp\Psr7\Response as PsrBaseResponse;
use Phalcon\Http\Response as BaseResponse;
use PhalconPlus\Base\Exception as BaseException;
use Psr\Http\Message\ResponseInterface;

class PsrResponseFactory
{
    protected psrResponse = null;

    public static function create(<BaseResponse> response, var protocol = "1.1") -> <ResponseInterface>
    {
        var status = response->getStatusCode();
        var reason = response->getReasonPhrase();
        var content = response->getContent();
        // ob_start();
        // response->send();
        // var content = ob_get_clean();
        // if !empty stdout {
        //     let content = stdout . content;
        // }
        var headers = self::mapHeaders(response);

        if !class_exists("GuzzleHttp\\Psr7\\Response") {
            throw new BaseException("We use GuzzleHttp\\Psr7\\Response, please include that package");
        }
        var psrResponse;
        let psrResponse = new PsrBaseResponse(
            status ? status : 200, 
            headers, 
            content, 
            protocol, 
            reason ? reason : "OK"
        );

        if !isset(headers["Content-Length"]) {
            psrResponse->withAddedHeader("Content-Length", strlen(content));
        }
        return psrResponse;
    }

    protected static function mapHeaders(<BaseResponse> response) -> array
    {
        var headers = response->getHeaders()->toArray();
        
        var rawHeaders = headers_list();
        var h, pos, nativeHeaders = [];
        for h in rawHeaders {
            let pos = strpos(h, ":");
            if false !== pos {
                var name = substr(h, 0, pos);
                var value = trim(substr(h, pos + 1));
                if isset(nativeHeaders[name]) {
                    if !is_array(nativeHeaders[name]) {
                        let nativeHeaders[name] = [nativeHeaders[name]];
                    }
                    let nativeHeaders[name][] = value;
                } else {
                    let nativeHeaders[name] = value;
                }
            }
        }
        // after reading all headers we need to reset it, so next request
        // operates on a clean header.
        header_remove();
        let headers = array_merge(nativeHeaders, headers);
        
        if isset(nativeHeaders["Set-Cookie"]) {
            let headers["Set-Cookie"] = is_array(nativeHeaders["Set-Cookie"])?nativeHeaders["Set-Cookie"]:[nativeHeaders["Set-Cookie"]];
        } else {
            let headers["Set-Cookie"] = null;
        }
        return headers;
    }
}</pre>
                </div>
            </div>
        </div>
    </body>
</html>
